package com.zxw.设计模式.结构型.装饰器模式.实际场景中的Service方法;

import com.zxw.设计模式.结构型.装饰器模式.抽象构建角色.DataLoader;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * @projectName: util-cloud
 * @package: com.zxw.设计模式.结构型.装饰器模式.具体构建类
 * @className: BaseFileDataLodeer
 * @author: zhangxuewei
 * @description: 正常业务的实现
 * @date: 2023/7/1 17:14
 * @version: 1.0
 */
public class BaseFileDataLoder implements DataLoader {

    private String filePath;

    public BaseFileDataLoder(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public String read() {
        try {
            String res = FileUtils.readFileToString(new File(filePath), "utf-8");
            return res;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void write(String data) {
        try {
            FileUtils.writeStringToFile(new File(filePath), data, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
